from game import zz_game
from zz import zz_log, zz_str

log = zz_log.of()


class Game(zz_game.ZZ_Game):
    def __init__(self, d):
        super().__init__(d)

    def get_name(self):
        return "青云诀之伏魔"

    def play_back(self, msg):
        """
        返回
        :param msg:
        """
        self.img_click_if_exists(msg, './game/img/青云诀之伏魔/back.png', './game/img/青云诀之伏魔/game_main.png')

    def play_bao(self):
        """
        熔炼
        :return:
        """
        self.img_click_if_exists('点击背包', './game/img/青云诀之伏魔/bao.png', './game/img/青云诀之伏魔/bao01.png')
        self.img_click_if_exists('点击熔炼', './game/img/青云诀之伏魔/bao01.png', './game/img/青云诀之伏魔/bao_alert.png')

        self.img_click('./game/img/青云诀之伏魔/bao01.png', 2)
        self.log_msg('点击一键熔炼')
        self.img_click('./game/img/青云诀之伏魔/bao_play.png', 2)
        self.d.sleep(2)
        self.click_bottom()

        self.play_back('熔炼-完成')

    def play_day_get_prize(self):
        self.img_click_if_exists('点击日常', './game/img/青云诀之伏魔/play_day.png', './game/img/青云诀之伏魔/play_day_05.png')
        # self.game_alert()

        self.log_msg('点击每日历练')
        self.img_click('./game/img/青云诀之伏魔/play_day_01.png', 2)
        self.log_msg("领取每日历练奖励")
        while True:
            if self.img_click('./game/img/青云诀之伏魔/play_get.png', 3):
                self.log_msg("领取成功")
                self.click_bottom()
                continue
            break
        self.log_msg('点击宝箱')
        self.img_click('./game/img/青云诀之伏魔/play_day_01_get.png', 2)
        self.click_bottom()
        self.log_msg("领取每日历练奖励-完成")

        self.img_click_if_exists('点击降妖伏魔', './game/img/青云诀之伏魔/play_day_03.png', './game/img/青云诀之伏魔/play_day_03_main.png')
        self.log_msg("领取降妖伏魔奖励")
        while True:
            if self.img_click('./game/img/青云诀之伏魔/play_get01.png', 3):
                self.log_msg("领取成功")
                continue

            if self.img_click('./game/img/青云诀之伏魔/use_0_01.png', 3):
                self.log_msg("点击使用")
                continue
            break
        self.log_msg("领取降妖伏魔奖励-完成")

        self.img_click_if_exists('点击资产找回', './game/img/青云诀之伏魔/play_day_04.png', './game/img/青云诀之伏魔/play_day_04_main.png')
        self.log_msg('点击免费找回')
        self.img_click('./game/img/青云诀之伏魔/play_day_04_get.png')
        self.log_msg('点击确认')
        self.img_click('./game/img/青云诀之伏魔/play_day_04_1.png')
        self.log_msg('点击资产找回-完成')

        self.img_click_if_exists('点击奇遇', './game/img/青云诀之伏魔/play_day_05.png', './game/img/青云诀之伏魔/play_day_05_main.png')
        self.log_msg("领取奇遇奖励")
        while True:
            if self.img_click('./game/img/青云诀之伏魔/play_get.png', 3):
                self.log_msg("领取成功")
                continue
            break

        # self.game_alert()
        self.log_msg("领取奇遇奖励-完成")
        self.play_back('日常奖励领取-完成')

    def play_day_05(self):
        """
        日常-奇遇-领任务
        """
        self.img_click_if_exists('点击日常', './game/img/青云诀之伏魔/play_day.png', './game/img/青云诀之伏魔/play_day_05.png')
        self.log_msg("点击奇遇")
        self.img_click('./game/img/青云诀之伏魔/play_day_05.png')
        self.log_msg("开始领取任务")
        while True:
            if self.img_click('./game/img/青云诀之伏魔/play_day_05_get.png', 2):
                self.log_msg("领取任务成功")
                continue
            break
        self.play_back("奇遇领任务完成")

    def play_room_duo(self):
        """
        9幽副本
        """
        self.log_msg("点击副本")
        self.img_click('./game/img/青云诀之伏魔/play_room.png')
        self.img_click_if_exists("点击多人副本", './game/img/青云诀之伏魔/play_room_duo_00.png', './game/img/青云诀之伏魔/play_room_duo_01.png')
        self.img_click_if_exists("点击9幽副本", './game/img/青云诀之伏魔/play_room_duo_01.png', './game/img/青云诀之伏魔/play_room_duo_02.png')
        self.log_msg("点击快速匹配")
        self.img_click('./game/img/青云诀之伏魔/play_room_duo_02.png')
        self.log_msg("等待进入战斗界面")
        if not self.img_exit('./game/img/青云诀之伏魔/play_room_duo_03.png', 20):
            self.log_msg("匹配失败")
            return False
        self.log_msg("已进入战斗界面")
        index = 0
        while not self.img_exit('./game/img/青云诀之伏魔/play_room_duo_done.png', 3):
            self.log_msg("战斗中")
            if index < 2:
                self.game_alert()
            self.d.sleep(10)
            index += 1

        self.log_msg("战斗完成，点击空白退出")
        # self.click_bottom()
        # self.d.sleep(2)
        self.play_back("9幽副本任务完成")

        return True

    def play_room_ta(self):
        """
        封魔古塔
        """
        self.log_msg("点击副本")
        self.img_click('./game/img/青云诀之伏魔/play_room.png')
        self.img_click_if_exists("点击单人副本", './game/img/青云诀之伏魔/play_room_ta_00.png', './game/img/青云诀之伏魔/play_room_ta_01.png')
        self.img_click_if_exists("点击封魔古塔", './game/img/青云诀之伏魔/play_room_ta_01.png', './game/img/青云诀之伏魔/play_room_ta_02.png')
        self.log_msg("点击挑战")
        self.img_click('./game/img/青云诀之伏魔/play_room_ta_02.png')
        if self.img_exit('./game/img/青云诀之伏魔/play_day_04_1.png', 1):
            self.log_msg("存在弹窗，点击确认")
            self.img_click('./game/img/青云诀之伏魔/play_day_04_1.png')

        while not self.img_exit('./game/img/青云诀之伏魔/play_room_ta_done.png', 2):
            self.log_msg("战斗中")
            self.d.sleep(2)
        self.log_msg("战斗完成，点击退出")
        self.img_click('./game/img/青云诀之伏魔/play_room_ta_done.png')
        self.play_back("封魔古塔任务完成")

    def play_room(self):
        """
        单人副本
        """
        self.log_msg("点击副本")
        self.img_click('./game/img/青云诀之伏魔/play_room.png')
        self.img_click_if_exists("点击每日试炼", './game/img/青云诀之伏魔/play_room01.png', './game/img/青云诀之伏魔/play_room_main.png')
        # self.log_msg("等待进入每日试炼")
        # self.img_exit('./game/img/青云诀之伏魔/play_room_main.png', 30)
        self.log_msg("已进入每日试炼")
        while True:
            self.log_msg("查看副本")

            do_path = ''
            if self.img_exit('./game/img/青云诀之伏魔/play_room02.png'):
                do_path = './game/img/青云诀之伏魔/play_room02.png'
            if self.img_exit('./game/img/青云诀之伏魔/play_room03.png'):
                do_path = './game/img/青云诀之伏魔/play_room03.png'
            if zz_str.has_any_text(do_path):
                self.log_msg(f"点击挑战-{do_path.split('/')[-1:]}")
                self.img_click(do_path)
                while not self.img_exit('./game/img/青云诀之伏魔/play_done.png', 3):
                    self.log_msg("副本挑战中")
                    self.d.sleep(2)
                self.log_msg("挑战完成")
                self.click_bottom()

                self.game_use()
                continue
            break

        self.play_back('副本挑战完成，点击返回')

    def play_boss(self):
        """
        boss
        """
        self.img_click_if_exists("点击boss", './game/img/青云诀之伏魔/play_boss.png', './game/img/青云诀之伏魔/play_boss_main.png')
        while True:
            self.log_msg("查看挑战boss")
            if self.img_exit('./game/img/青云诀之伏魔/play_room02.png'):
                self.log_msg("点击挑战")
                self.img_click('./game/img/青云诀之伏魔/play_room02.png')
                self.d.sleep(1)
                self.log_msg("检查是否进入挑战")
                if self.img_exit('./game/img/青云诀之伏魔/play_boss_main.png', 1):
                    self.log_msg("进入挑战-失败，退出")
                    break

                while not self.img_exit('./game/img/青云诀之伏魔/play_done.png', 3):
                    self.log_msg("boss挑战中")
                self.log_msg("挑战完成")
                self.click_bottom()
                continue
            break

        self.play_back('boss挑战完成，点击返回')

    def game_alert(self):
        self.alert_close_by_file('./game/img/青云诀之伏魔')
        self.game_use()

    def game_use(self):
        self.alert_close_by_file('./game/img/青云诀之伏魔', 0.9, 'use_')

    def play1(self):
        """
        闯1关
        """
        self.log_msg("开始闯关")
        self.log_msg("获取自动闯关位置")
        r = self.find_img('./game/img/青云诀之伏魔/play_auto.png')
        if r.err():
            self.log_msg("获取自动闯关位置失败")
            return
        x, y = r.get_data("center")  # 取消自动用

        play_fail = False
        self.img_click_if_exists('点击自动闯关', './game/img/青云诀之伏魔/play_auto.png', './game/img/青云诀之伏魔/play_auto02.png')
        while True:
            self.log_msg("闯关中")

            if self.img_exit('./game/img/青云诀之伏魔/play_auto_done.png', 2):
                self.log_msg("闯关完成")
                self.click_bottom()
                break

            if self.img_exit('./game/img/青云诀之伏魔/play_auto_done_01.png', 2):
                self.log_msg("闯关失败")
                play_fail = True
                break

        if not play_fail:
            self.d.sleep(1)
            self.log_msg("点击取消自动闯关")
            self.d.click(x, y)
            self.d.sleep(2)
            while self.img_exit('./game/img/青云诀之伏魔/play_auto_done.png', 2):
                self.log_msg("取消自动闯关失败，继续尝试")
                self.d.click(x, y)
                self.d.sleep(2)
            self.log_msg("取消自动闯关-成功")
        else:
            self.game_alert()

    def handle_main(self):
        self.log_msg("等待进入登陆界面")
        if not self.img_exit('./game/img/青云诀之伏魔/game_start_main.png', 30):
            return False
        self.log_msg("已进入登陆界面")
        self.log_msg("点击登录游戏")
        self.img_click('./game/img/青云诀之伏魔/game_start.png')
        self.log_msg("等待游戏主界面")
        self.img_exit('./game/img/青云诀之伏魔/game_main.png', 100)
        self.log_msg("已游戏主界面")
        # 弹窗处理
        self.game_alert()

        for i in range(2):
            self.play_room_ta()
        for i in range(2):
            self.play_room_duo()
        self.d.sleep(2)
        self.get_fu()
        # self.play_room()  # 打副本

        """    
        self.play_bao()  # 熔炼背包装备
        self.game_alert()
        self.play_day_05()  # 领日常任务

        play_count = 0
        while not self.check_done():
            if play_count >= 8:
                self.log_msg("一直挑战失败，跳出")
                break
            self.play1()
            play_count += 1

        self.game_alert()
        self.play_boss()  # 打boss
        self.game_alert()
        # self.play_room()  # 打副本
        # self.game_alert()
        self.play_boss()  # 再打boss，防止没打完
        self.game_alert()
        self.play_day_get_prize()  # 领奖励
        # self.d.sleep(2)
        #        self.close_game()
        """
        return True
